import requests
import pygal
from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
#执行api调用并存储结果
url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
r = requests.get(url)
#print("Status Code: "+ str(r.status_code))
#将响应结果存储在json里面
response_dict = r.json()
#print(response_dict.keys())
#探索有关仓库的信息
repo_dicts = response_dict['items']
# print("Repositories :" ,len(repo_dicts))
#研究第一个仓库
repo_dict = repo_dicts[0]
print('项目名:'+repo_dict['name'])
print('所有者:'+repo_dict['owner']['login'])
print('加星数:',repo_dict['stargazers_count'])
print('仓库链接:'+repo_dict['html_url'])
print('创建时间:'+repo_dict['created_at'])
print('更新时间:'+repo_dict['updated_at'])
print('描述:'+repo_dict['description'])

names,plot_dicts = [],[]
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    description = '暂无描述'
    if repo_dict['description']:
        description = repo_dict['description']
    plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': description,
        'xlink': repo_dict['html_url'],
    }
    plot_dicts.append(plot_dict)
#可视化
my_style = LS('#333366',base_style=LCS)
#自定义配置
my_config = pygal.Config()
my_config.x_label_rotation = 45 # x轴坐标倾斜45度
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = False #不展示图标中的水平虚线
my_config.width = 1000
chart = pygal.Bar(my_config,style=my_style)
chart.title = 'GitHub上的最受欢迎的Python项目'
chart.x_labels = names
chart.add('',plot_dicts)
chart.render_to_file('python_repos.svg')

